home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / windows / pgpcrakw.zip / IDEA.C < prev    next >
C/C++ Source or Header  |  1996-04-27  |  17KB  |  593 lines

  1. /*
  2.  *    idea.c - C source code for IDEA block cipher.
  3.  *      IDEA (International Data Encryption Algorithm), formerly known as 
  4.  *      IPES (Improved Proposed Encryption Standard).
  5.  *      Algorithm developed by Xuejia Lai and James L. Massey, of ETH Zurich.
  6.  *      This implementation modified and derived from original C code 
  7.  *      developed by Xuejia Lai.  
  8.  *      Zero-based indexing added, names changed from IPES to IDEA.
  9.  *      CFB functions added.  Random number routines added.
  10.  *
  11.  *      Extensively optimized and restructured by Colin Plumb.
  12.  *
  13.  *      There are two adjustments that can be made to this code to
  14.  *      speed it up.  Defaults may be used for PCs.  Only the -DIDEA32
  15.  *      pays off significantly if selectively set or not set.
  16.  *      Experiment to see what works best for your machine.
  17.  *
  18.  *      Multiplication: default is inline, -DAVOID_JUMPS uses a
  19.  *              different version that does not do any conditional
  20.  *              jumps (a few percent worse on a SPARC), while
  21.  *              -DSMALL_CACHE takes it out of line to stay
  22.  *              within a small on-chip code cache.
  23.  *      Variables: normally, 16-bit variables are used, but some
  24.  *              machines (notably RISCs) do not have 16-bit registers,
  25.  *              so they do a great deal of masking.  -DIDEA32 uses "int"
  26.  *              register variables and masks explicitly only where
  27.  *              necessary.  On a SPARC, for example, this boosts
  28.  *              performace by 30%.
  29.  *
  30.  *      The IDEA(tm) block cipher is covered by patents held by ETH and a
  31.  *      Swiss company called Ascom-Tech AG.  The Swiss patent number is
  32.  *      PCT/CH91/00117, the European patent number is EP 0 482 154 B1, and
  33.  *      the U.S. patent number is US005214703.  IDEA(tm) is a trademark of
  34.  *      Ascom-Tech AG.  There is no license fee required for noncommercial
  35.  *      use.  Commercial users may obtain licensing details from Dieter
  36.  *      Profos, Ascom Tech AG, Solothurn Lab, Postfach 151, 4502 Solothurn,
  37.  *      Switzerland, Tel +41 65 242885, Fax +41 65 235761.
  38.  *
  39.  *      The IDEA block cipher uses a 64-bit block size, and a 128-bit key 
  40.  *      size.  It breaks the 64-bit cipher block into four 16-bit words
  41.  *      because all of the primitive inner operations are done with 16-bit 
  42.  *      arithmetic.  It likewise breaks the 128-bit cipher key into eight 
  43.  *      16-bit words.
  44.  *
  45.  *      For further information on the IDEA cipher, see the book:
  46.  *        Xuejia Lai, "On the Design and Security of Block Ciphers",
  47.  *        ETH Series on Information Processing (ed. J.L. Massey) Vol 1,
  48.  *        Hartung-Gorre Verlag, Konstanz, Switzerland, 1992.  ISBN
  49.  *        3-89191-573-X.
  50.  *
  51.  *      This code runs on arrays of bytes by taking pairs in big-endian
  52.  *      order to make the 16-bit words that IDEA uses internally.  This
  53.  *      produces the same result regardless of the byte order of the
  54.  *      native CPU.
  55.  */
  56.  
  57. #include <string.h>
  58. #include "idea.h"
  59.  
  60. #ifdef MACTC5
  61. #include <string.h>
  62. #define IDEA32
  63. #define SMALL_CACHE
  64. #define USE68ASM
  65. void ideaCipher(byte const inbuf[8], byte outbuf[8],
  66.                word16 const *key);
  67. #endif
  68.  
  69. #ifdef IDEA32            /* Use >16-bit temporaries */
  70. #define low16(x) ((x) & 0xFFFF)
  71. typedef unsigned int uint16;    /* at LEAST 16 bits, maybe more */
  72. #else
  73. #define low16(x) (x)        /* this is only ever applied to uint16's */
  74. typedef word16 uint16;
  75. #endif
  76.  
  77. #ifdef _GNUC_
  78. /* __const__ simply means there are no side effects for this function,
  79.  * which is useful info for the gcc optimizer
  80.  */
  81. #define CONST __const__
  82. #else
  83. #define CONST
  84. #endif
  85.  
  86. /*
  87.  * Multiplication, modulo (2**16)+1
  88.  * Note that this code is structured on the assumption that
  89.  * untaken branches are cheaper than taken branches, and the
  90.  * compiler doesn't schedule branches.
  91.  */
  92. #ifdef SMALL_CACHE
  93. #ifdef MACTC5
  94.  
  95. CONST static uint16 
  96. mul(uint16 a, uint16 b) {
  97. asm {
  98.         move.w    a,d1
  99.         beq.s    @aeq0
  100.         move.w    b,d0
  101.         beq.s    @beq0
  102.         mulu.w    d0,d1    ; a = a * b
  103.         move.w    d1,d0    ; b = a
  104.         swap    d1        ; a = a >> 16
  105.         sub.w    d1,d0    ; b = b - a
  106.         bcc.s    @endit    ; b >= a?
  107.         addq.w    #1,d0    ; b < a : (b - a) + 1
  108.         bra.s    @endit
  109. @aeq0:    moveq    #1,d0
  110.         move.w    b,d1
  111.         sub.w    d1,d0    ; 1 - b
  112.         bra.s    @endit
  113. @beq0:    moveq    #1,d0
  114.         sub.w    d1,d0    ; 1 - a
  115.         }
  116. endit:    return;
  117. }
  118.  
  119. #else
  120.  
  121. CONST static uint16
  122.  mul(register uint16 a, register uint16 b)
  123. {
  124.     register word32 p;
  125.  
  126.     p = (word32) a *b;
  127.     if (p) {
  128.     b = low16(p);
  129.     a = p >> 16;
  130.     return (b - a) + (b < a);
  131.     } else if (a) {
  132.     return 1 - a;
  133.     } else {
  134.     return 1 - b;
  135.     }
  136. }                /* mul */
  137. #endif            /* MACTC5 */
  138. #endif            /* SMALL_CACHE */
  139.  
  140. /*
  141.  * Compute the multiplicative inverse of x, modulo 65537, using Euclid's
  142.  * algorithm. It is unrolled twice to avoid swapping the registers each
  143.  * iteration, and some subtracts of t have been changed to adds.
  144.  */
  145. CONST static uint16
  146.  mulInv(uint16 x)
  147. {
  148.     uint16 t0, t1;
  149.     uint16 q, y;
  150.  
  151.     if (x <= 1)
  152.     return x;        /* 0 and 1 are self-inverse */
  153.     t1 = 0x10001L / x;        /* Since x >= 2, this fits into 16 bits */
  154.     y = 0x10001L % x;
  155.     if (y == 1)
  156.     return low16(1 - t1);
  157.     t0 = 1;
  158.     do {
  159.     q = x / y;
  160.     x = x % y;
  161.     t0 += q * t1;
  162.     if (x == 1)
  163.         return t0;
  164.     q = y / x;
  165.     y = y % x;
  166.     t1 += q * t0;
  167.     } while (y != 1);
  168.     return low16(1 - t1);
  169. }                /* mukInv */
  170.  
  171. /*
  172.  * Expand a 128-bit user key to a working encryption key EK
  173.  */
  174. static void ideaExpandKey(byte const *userkey, word16 * EK)
  175. {
  176.     int i, j;
  177.  
  178.     for (j = 0; j < 8; j++) {
  179.     EK[j] = (userkey[0] << 8) + userkey[1];
  180.     userkey += 2;
  181.     }
  182.     for (i = 0; j < IDEAKEYLEN; j++) {
  183.     i++;
  184.     EK[i + 7] = EK[i & 7] << 9 | EK[i + 1 & 7] >> 7;
  185.     EK += i & 8;
  186.     i &= 7;
  187.     }
  188. }                /* ideaExpandKey */
  189.  
  190. /*
  191.  * Compute IDEA decryption key DK from an expanded IDEA encryption key EK
  192.  * Note that the input and output may be the same.  Thus, the key is
  193.  * inverted into an internal buffer, and then copied to the output.
  194.  */
  195. static void ideaInvertKey(word16 const *EK, word16 DK[IDEAKEYLEN])
  196. {
  197.     int i;
  198.     uint16 t1, t2, t3;
  199.     word16 temp[IDEAKEYLEN];
  200.     word16 *p = temp + IDEAKEYLEN;
  201.  
  202.     t1 = mulInv(*EK++);
  203.     t2 = -*EK++;
  204.     t3 = -*EK++;
  205.     *--p = mulInv(*EK++);
  206.     *--p = t3;
  207.     *--p = t2;
  208.     *--p = t1;
  209.  
  210.     for (i = 0; i < IDEAROUNDS - 1; i++) {
  211.     t1 = *EK++;
  212.     *--p = *EK++;
  213.     *--p = t1;
  214.  
  215.     t1 = mulInv(*EK++);
  216.     t2 = -*EK++;
  217.     t3 = -*EK++;
  218.     *--p = mulInv(*EK++);
  219.     *--p = t2;
  220.     *--p = t3;
  221.     *--p = t1;
  222.     }
  223.     t1 = *EK++;
  224.     *--p = *EK++;
  225.     *--p = t1;
  226.  
  227.     t1 = mulInv(*EK++);
  228.     t2 = -*EK++;
  229.     t3 = -*EK++;
  230.     *--p = mulInv(*EK++);
  231.     *--p = t3;
  232.     *--p = t2;
  233.     *--p = t1;
  234. /* Copy and destroy temp copy */
  235.     memcpy(DK, temp, sizeof(temp));
  236.     burn(temp);
  237. }                /* ideaInvertKey */
  238.  
  239. /*
  240.  * MUL(x,y) computes x = x*y, modulo 0x10001.  Requires two temps, 
  241.  * t16 and t32.  x is modified, and must be a side-effect-free lvalue.
  242.  * y may be anything, but unlike x, must be strictly less than 65536 
  243.  * even if low16() is #defined.
  244.  * All of these are equivalent - see which is faster on your machine
  245.  */
  246. #ifdef SMALL_CACHE
  247. #define MUL(x,y) (x = mul(low16(x),y))
  248. #else                /* !SMALL_CACHE */
  249. #ifdef AVOID_JUMPS
  250. #define MUL(x,y) (x = low16(x-1), t16 = low16((y)-1), \
  251.         t32 = (word32)x*t16 + x + t16, x = low16(t32), \
  252.         t16 = t32>>16, x = (x-t16) + (x<t16) + 1)
  253. #else                /* !AVOID_JUMPS (default) */
  254. #define MUL(x,y) \
  255.     ((t16 = (y)) ? \
  256.         (x=low16(x)) ? \
  257.             t32 = (word32)x*t16, \
  258.             x = low16(t32), \
  259.             t16 = t32>>16, \
  260.             x = (x-t16)+(x<t16) \
  261.         : \
  262.             (x = 1-t16) \
  263.     : \
  264.         (x = 1-x))
  265. #endif
  266. #endif
  267.  
  268. /*      IDEA encryption/decryption algorithm */
  269. /* Note that in and out can be the same buffer */
  270. #ifndef USE68ASM
  271. static void ideaCipher(byte const inbuf[8], byte outbuf[8],
  272.                word16 const *key)
  273. {
  274.     register uint16 x1, x2, x3, x4, s2, s3;
  275.     word16 *in, *out;
  276. #ifndef SMALL_CACHE
  277.     register uint16 t16;    /* Temporaries needed by MUL macro */
  278.     register word32 t32;
  279. #endif
  280.     int r = IDEAROUNDS;
  281.  
  282.     in = (word16 *) inbuf;
  283.     x1 = *i